package jmetal.operators.selection;

import java.util.Comparator;
import java.util.HashMap;

import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.util.PseudoRandom;
import jmetal.util.comparators.DominanceComparator;

public class BinaryTournamentDirFitness extends Selection {

	private static final long serialVersionUID = -7005736010978446057L;

	/**
	 * dominance_ store the <code>Comparator</code> for check dominance_
	 */
	
	@SuppressWarnings("rawtypes")
	private Comparator dominance_;

	/**
	 * a_ stores a permutation of the solutions in the solutionSet used
	 */
	private int a_[];

	/**
	 * index_ stores the actual index for selection
	 */
	private int index_ = 0;

	public BinaryTournamentDirFitness(HashMap<String, Object> parameters) {
		super(parameters);
		dominance_ = new DominanceComparator();
	} // BinaryTournament2

	/**
	 * Performs the operation
	 * 
	 * @param object
	 *            Object representing a SolutionSet
	 * @return the selected solution
	 */
	public Object execute(Object object) {
		SolutionSet population = (SolutionSet) object;
		if (index_ == 0) // Create the permutation
		{
			a_ = (new jmetal.util.PermutationUtility()).intPermutation(population.size());
		}

		Solution solution1, solution2;
		solution1 = population.get(a_[index_]);
		solution2 = population.get(a_[index_ + 1]);

		index_ = (index_ + 2) % population.size();

		@SuppressWarnings("unchecked")
		int flag = dominance_.compare(solution1, solution2);
		if (flag == -1)
			return solution1;
		else if (flag == 1)
			return solution2;
		else if (solution1.getFitness() < solution2.getFitness())
			return solution1;
		else if (solution2.getFitness() < solution1.getFitness())
			return solution2;
		else if (PseudoRandom.randDouble() < 0.5)
			return solution1;
		else
			return solution2;
	} // execute

}
